#include <iostream>
#include <functional>
#include <unistd.h>
#include "TcpSvr.hpp"
#include "Protocol.hpp"
#include "SvrCal.hpp"

using namespace std;


void test1()
{
    // 测试Request, 序列化 + 添加报头
    Request r(122223, 456, '*');
    string s;
    r.serialize(&s);
    s = encode(s);
    cout << s;

    // 去掉报头
    string out;
    decode(s, &out);
    cout << out << endl;
    
    // 反序列化
    Request tmp;
    tmp.deserialize(out);
    printf("%d %c %d\n", tmp._a, tmp._op, tmp._b);
    printf("===================================\n");
}

void test2()
{
    // 测试Reponse, 序列化 + 添加报头
    Response r(9999, 0);
    string s;
    r.serialize(&s);
    s = encode(s);
    cout << s;

    // 去掉报头
    string out;
    decode(s, &out);
    cout << out << endl;

    // 反序列化
    Response tmp;
    tmp.deserialize(out);
    printf("%d %d\n", tmp._res, tmp._exitCode);
    printf("===================================\n");
}

int main(int argc, char* argv[])
{
    if(argc != 2) {
        cout << "Usage error!\n";
        return -1;
    }

    uint16_t port = stoi(argv[1]);
    SvrCal cal;
    TcpSvr *svr = new TcpSvr(port, bind(&SvrCal::doCalculate, &cal, placeholders::_1));
    svr->initSvr();
    daemon(0, 0);
    svr->startSvr();
    return 0;
}